<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\api\controller;
use think\Db;
class Quan extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }


    /**
     * 获取顶部数据：indexTop
     * 企业
     */
    public function indexTop(){
        extract(input());
        //1、顶部企业信息
        $user = $this->getUserInfo($user_id,'user_type,user_hospital',2);
        if($user['user_type'] == 1){
            $top = Db::table('up_company')
                ->where('c_status',1)
                ->where('c_verify',2)
                ->field('c_id,c_name,c_logo,c_area_str,c_address')
                ->order('c_updatetime desc')
                ->select();
        }else{
            $top = Db::table('up_company')
                ->where('c_id',$user['user_hospital'])
                ->field('c_id,c_name,c_logo,c_area_str,c_address')
                ->find();
        }
        return json(['status'=>200,'msg'=>$top,'user_type'=>$user['user_type']]);
    }
    /**
     * 获取数据：indexData
     *
     * 3企业、4产品、5资料、6项目、1会议
     */
    public function indexData(){
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_auth,user_hospital,user_sheng,user_shi',2);
        $flag = false;
        //---------------------------------------------------------------------------------
        //企业
        $where = [
            'c_verify' => 2,
            'c_status' => 1
        ];
        if($user['user_type'] == 2){
            $where['c_id'] = $user['user_hospital'];
        }
        if(isset($keyword) && $keyword){
            $where['c_name'] = ['like','%' . $keyword . '%'];
        }
        $field = 'c_id id,c_name name,c_logo img,c_id uid,c_area_str one,c_address two';
        $field .= ',c_updatetime time,data_type,c_id start,c_id end';
        if(isset($type) && $type == 3){
            $data = Db::table('up_company')
                ->where($where)
                ->order('c_updatetime DESC')
                ->page($page,10)
                ->field($field)
                ->select();
            $flag = true;
        }
        $a = Db::table('up_company')
            ->where($where)
            ->field($field)
            ->buildSql();
        //产品
        $where = [
            'c_status' => 1,
            'cp_status' => 1
        ];
        if($user['user_type'] == 2){
            $where['c_id'] = $user['user_hospital'];
        }
        if(isset($keyword) && $keyword){
            $where['cp_name'] = ['like','%' . $keyword . '%'];
        }
        $field = 'cp_id id,cp_name name,cp_thumb img,cp_charge_uid uid,user_username one,user_image two';
        $field .= ',cp_update time,cp.data_type,cp_id start,cp_c_id end';
        if(isset($type) && $type == 4){
            $data = Db::table('up_company_product cp')
                ->join('up_company c','cp.cp_c_id=c.c_id','LEFT')
                ->join('up_u_user u','cp.cp_charge_uid=u.user_id','LEFT')
                ->where($where)
                ->order('cp_update DESC')
                ->page($page,10)
                ->field($field)
                ->select();
            $flag = true;
        }
        $b = Db::table('up_company_product cp')
            ->join('up_company c','cp.cp_c_id=c.c_id','LEFT')
            ->join('up_u_user u','cp.cp_charge_uid=u.user_id','LEFT')
            ->where($where)
            ->field($field)
            ->buildSql();
        //资料
        $where = [
            'c_status' => 1,
            'cp_status' => 1,
            'cpd_del' => 1,
        ];
        if($user['user_type'] == 2){
            $where['c_id'] = $user['user_hospital'];
        }else{
            $son = [];
            $son[] = ['like',"%,{$user['user_shi']},%"];
            $son[] = ['=',0];
            $where['cpd_shi'] = [$son,'OR'];
        }
        if(isset($keyword) && $keyword){
            $where['cpd_name'] = ['like','%' . $keyword . '%'];
        }
        $field = 'cpd_id id,cpd_name name,cpd_thumb img,cp_charge_uid uid,user_username one,user_image two';
        $field .= ',cpd_update time,cpd.data_type,cpd_cp_id start,cp_c_id end';
        if(isset($type) && $type == 5){
            $data = Db::table('up_company_product_data cpd')
                ->join('up_company_product cp','cpd.cpd_cp_id=cp.cp_id','LEFT')
                ->join('up_company c','cp.cp_c_id=c.c_id','LEFT')
                ->join('up_u_user u','cp.cp_charge_uid=u.user_id','LEFT')
                ->where($where)
                ->order('cp_update DESC')
                ->page($page,10)
                ->field($field)
                ->select();
            $flag = true;
        }
        $c = Db::table('up_company_product_data cpd')
            ->join('up_company_product cp','cpd.cpd_cp_id=cp.cp_id','LEFT')
            ->join('up_company c','cp.cp_c_id=c.c_id','LEFT')
            ->join('up_u_user u','cp.cp_charge_uid=u.user_id','LEFT')
            ->where($where)
            ->field($field)
            ->buildSql();
        //项目
        $where = [
            'c_status' => 1,
            'pro_del' => 1,
        ];
        if($user['user_type'] == 2){
            $where['c_id'] = $user['user_hospital'];
        }
        if(isset($keyword) && $keyword){
            $where['pro_name'] = ['like','%' . $keyword . '%'];
        }
        $field = 'pro_id id,pro_name name,pro_status img,pro_user_id uid,user_username one,user_image two';
        $field .= ',pro_update time,p.data_type,pro_start_time start,pro_end_time end';
        if(isset($type) && $type == 6){
            $data = Db::table('up_project p')
                ->join('up_company c','p.pro_c_id=c.c_id','LEFT')
                ->join('up_u_user u','p.pro_user_id=u.user_id','LEFT')
                ->where($where)
                ->order('pro_update DESC')
                ->page($page,10)
                ->field($field)
                ->select();
            $flag = true;
        }
        $d = Db::table('up_project p')
            ->join('up_company c','p.pro_c_id=c.c_id','LEFT')
            ->join('up_u_user u','p.pro_user_id=u.user_id','LEFT')
            ->where($where)
            ->field($field)
            ->buildSql();
        //会议:会议的地址和礼拜、状态在循环里做
        $where = [
            'm_status' => ['<>',4],
            'm_del' => 1
        ];
        if($user['user_type'] == 2){
            $where['user_hospital'] = $user['user_hospital'];
        }
        if(isset($keyword) && $keyword){
            $where['m_name'] = ['like','%' . $keyword . '%'];
        }
        $field = 'm_id id,m_name name,m_type img,m_user_id uid,user_username one,user_image two';
        $field .= ',m_update time,data_type,m_start start,m_end end';
        if(isset($type) && $type == 1){
            $field .= ',m_address,m_week,m_from';
            $data = Db::table('up_meeting m')
                ->join('up_u_user u','m.m_user_id=u.user_id','LEFT')
                ->where($where)
                ->order('m_update DESC')
                ->page($page,10)
                ->field($field)
                ->select();
            $flag = true;
        }
        if($user['user_type'] == 1){
            $e = Db::table('up_meeting m')
                ->join('up_u_user u','m.m_user_id=u.user_id','LEFT')
                ->where($where)
                ->field($field)
                ->union([$a,$b,$c,$d])
                ->buildSql();
        }else{
            $e = Db::table('up_meeting m')
                ->join('up_u_user u','m.m_user_id=u.user_id','LEFT')
                ->where($where)
                ->field($field)
                ->union([$b,$c,$d])
                ->buildSql();
        }
        if($flag === false){
            $data = Db::table($e . ' a')->order("time desc")->page($page,10)->select();// $c . a是零时表名
        }
        if($data){
            //循环里再跑一遍数据库吧
            foreach ($data as $k => $v){
                if($v['data_type'] == 1){
                    if($flag === false) {
                        $meet = Db::table('up_meeting')->where('m_id', $v['id'])
                            ->field('m_address,m_week,m_from')->find();
                        $data[$k]['m_address'] = $meet['m_address'];
                        $data[$k]['m_week'] = $meet['m_week'];
                        $data[$k]['m_from'] = $meet['m_from'];
                    }
                    //会议是否参加
                    $join = Db::table('up_meeting_user')
                        ->where('mu_user_id', $user_id)
                        ->where('mu_m_id', $v['id'])
                        ->field('mu_status')
                        ->find();
                    $data[$k]['join'] = 0; //未参加
                    if($join){
                        $data[$k]['join'] = $join['mu_status']; //1是待审核  2是同意  3是拒绝
                    }
                }
                //收藏：资料、项目、会议
                if($v['data_type'] == 5 || $v['data_type'] == 6 || $v['data_type'] == 1){
                    if($v['data_type'] == 5){ $like_type = 1; }
                    if($v['data_type'] == 6){ $like_type = 2; }
                    if($v['data_type'] == 1){ $like_type = 3; }
                    $like = Db::table('up_like_project')
                        ->where('lpro_user_id', $user_id)
                        ->where('lpro_type', $like_type)
                        ->where('lpro_prom_id', $v['id'])
                        ->find();
                    $data[$k]['like'] = 0;
                    if($like){
                        $data[$k]['like'] = 1;
                    }
                }
                //谁才是真正的负责人
                if($v['data_type'] == 4 || $v['data_type'] == 5){
                    $realdata = $this->productRealCharger($v['start'],$user_id,$v['end']);
                    $data[$k]['uid'] = '';
                    $data[$k]['one'] = '';
                    $data[$k]['two'] = '';
                    if($realdata){
                        $data[$k]['uid'] = $realdata['user_id'];
                        $data[$k]['one'] = $realdata['user_username'];
                        $data[$k]['two'] = $realdata['user_image'];
                    }
                }
            }
        }
        return json(['status'=>200,'msg'=>$data]);
    }

    /**
     * 企业详情
     */
    public function companyInfo(){
        extract(input());
        $data = Db::table('up_company')
            ->where('c_id',$c_id)
            ->field('c_id,c_name,c_logo,c_area_str,c_address,c_content')
            ->find();
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 企业产品列表
     */
    public function companyProduct(){
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_auth,user_hospital,user_sheng,user_shi',2);
        $data = Db::table('up_company_product cp')
            ->join('up_u_user u','cp.cp_charge_uid=u.user_id','LEFT')
            ->where('cp_c_id',$c_id)
            ->where('cp_status',1)
            ->order('cp_update desc')
            ->field('cp.*,user_id,user_username,user_image')
            ->page($page,10)
            ->select();
        if($data){
            foreach ($data as $k => $v){
                $realdata = $this->productRealCharger($v['cp_id'],$user_id,$v['cp_c_id']);
                $data[$k]['user_id'] = '';
                $data[$k]['user_username'] = '';
                $data[$k]['user_image'] = '';
                if($realdata){
                    $data[$k]['user_id'] = $realdata['user_id'];
                    $data[$k]['user_username'] = $realdata['user_username'];
                    $data[$k]['user_image'] = $realdata['user_image'];
                }
            }
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 产品详情（基础数据）
     */
    public function productInfo(){
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_auth,user_hospital,user_sheng,user_shi',2);
        $data = Db::table('up_company_product cp')
            ->join('up_u_user u','cp.cp_charge_uid=u.user_id','LEFT')
            ->where('cp_id',$cp_id)
            ->field('cp.*,user_id,user_username,user_image')
            ->find();
        $realdata = $this->productRealCharger($data['cp_id'],$user_id,$data['cp_c_id']);
        $data['user_id'] = '';
        $data['user_username'] = '';
        $data['user_image'] = '';
        if($realdata){
            $data['user_id'] = $realdata['user_id'];
            $data['user_username'] = $realdata['user_username'];
            $data['user_image'] = $realdata['user_image'];
        }
        //自增浏览量
        Db::table('up_company_product')
            ->where('cp_id',$cp_id)
            ->setInc('cp_browse');
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 产品配套数据：
     * type：1资料、2项目、3会议
     */
    public function productDatas(){
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_shi',2);
        if($type == 1){
            $where = [
                'cpd_cp_id' => $cp_id,
                'cpd_del' => 1
            ];
            if($user['user_type'] == 1){
                $son = [];
                $son[] = ['like',"%,{$user['user_shi']},%"];
                $son[] = ['=',0];
                $where['cpd_shi'] = [$son,'OR'];
            }
            $data = Db::table('up_company_product_data')
                ->where($where)
                ->order('cpd_top desc,cpd_sort desc,cpd_update desc')
                ->page($page,10)
                ->select();
            if($data){
                foreach ($data as $k => $v){
                    if($v['cpd_type'] == 1 && $v['cpd_content']){
                        $data[$k]['cpd_content'] = explode(',',$v['cpd_content']);
                    }
                }
            }
        }else if($type == 2){
            $data = Db::table('up_project')
                ->where('pro_cp_id',$cp_id)
                ->where('pro_del',1)
                ->order('pro_update desc')
                ->page($page,10)
                ->select();
        }else{
            $data = $e = Db::table('up_meeting m')
                ->join('up_u_user u','m.m_user_id=u.user_id','LEFT')
                ->where('m_cp_id',$cp_id)
                ->where('m_type','in','1,3')
                ->where('m_del',1)
                ->order('m_status asc,m_start asc')
                ->page($page,10)
                ->field('m.*,user_id,user_username,user_image')
                ->select();
            if($data){
                foreach ($data as $k => $v){
                    //会议是否参加
                    $join = Db::table('up_meeting_user')
                        ->where('mu_user_id', $user_id)
                        ->where('mu_m_id', $v['m_id'])
                        ->field('mu_status')
                        ->find();
                    $data[$k]['join'] = 0; //未参加
                    if($join){
                        $data[$k]['join'] = $join['mu_status']; //1是待审核  2是同意  3是拒绝
                    }
                }
            }
        }
        return json(['status'=>200,'msg'=>$data]);
    }

    /**
     * 资料详情
     *
     */
    public function productDataInfo()
    {
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_auth,user_hospital,user_sheng,user_shi',2);
        $field = 'cpd.*,cp_id,cp_name,cp_thumb,c_id,c_name,c_logo,user_id,user_username,user_image';
        $data = Db::table('up_company_product_data cpd')
            ->join('up_company_product cp','cpd.cpd_cp_id=cp.cp_id','LEFT')
            ->join('up_company c','cp.cp_c_id=c.c_id','LEFT')
            ->join('up_u_user u','cp.cp_charge_uid=u.user_id','LEFT')
            ->where('cpd_id',$cpd_id)
            ->field($field)
            ->find();
        if($data['cpd_type'] == 1 && $data['cpd_content']){
            $data['cpd_content'] = explode(',',$data['cpd_content']);
        }
        if($data['cpd_type'] == 2){
            $data['cpd_content'] = [];
            if($data['cpd_pdf_img']){
                $data['cpd_content'] = explode(',',$data['cpd_pdf_img']);
            }
        }
        $like = Db::table('up_like_project')
            ->where('lpro_user_id', $user_id)
            ->where('lpro_type', 1)
            ->where('lpro_prom_id', $cpd_id)
            ->find();
        $data['like'] = 0;
        if($like){
            $data['like'] = 1;
        }
        $realdata = $this->productRealCharger($data['cp_id'],$user_id,$data['c_id']);
        $data['user_id'] = '';
        $data['user_username'] = '';
        $data['user_image'] = '';
        if($realdata){
            $data['user_id'] = $realdata['user_id'];
            $data['user_username'] = $realdata['user_username'];
            $data['user_image'] = $realdata['user_image'];
        }
        //自增浏览量
        Db::table('up_company_product_data')
            ->where('cpd_id',$cpd_id)
            ->setInc('cpd_browse');
        //入浏览记录(以天为单位)
        $i_h = [
            'd_id' => $cpd_id,
            'c_user_id' => $user_id,
            'date' => date('Y-m-d')
        ];
        $have = Db::table('up_zz_history')
            ->where($i_h)
            ->find();
        if($have){
            Db::table('up_zz_history')->where('id',$have['id'])->setInc('times');
        }else{
            $i_h['c_id'] = $data['c_id'];
            $i_h['p_id'] = $data['cp_id'];
            Db::table('up_zz_history')->insert($i_h);
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 资料提问
     */
    public function productDataQuestion()
    {
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_auth,user_hospital,user_sheng,user_shi',2);
        if($user['user_type'] == 2){
            return json(['status'=>100,'msg'=>'您无权提问']);
        }
        $today = date('Y-m-d H:i:s');
        $data = Db::table('up_company_product_data cpd')
            ->join('up_company_product cp','cpd.cpd_cp_id=cp.cp_id','LEFT')
            ->where('cpd_id',$cpd_id)
            ->field('cp_id,cp_c_id')
            ->find();
        $realdata = $this->productRealCharger($data['cp_id'],$user_id,$data['cp_c_id']);
        $data['user_id'] = '';
        $data['user_username'] = '';
        $data['user_image'] = '';
        if($realdata){
            $uid = $realdata['user_id'];
        }else{
            $uid = '';
        }
        if(!$uid){
            return json(['status'=>100,'msg'=>'此资料所属产品暂无负责人，请联系管理员']);
        }
        Db::startTrans();
        try{
            $insert = [
                'dq_cpd_id' => $cpd_id,
                'dq_send_uid' => $user_id,
                'dq_get_uid' => $uid,
                'dq_content' => $content,
                'dq_imgs' => trim($image,','),
                'dq_all_uid' => ',' . $uid . ',',
                'dq_time' => $today
            ];
            $qid = Db::table('up_company_pd_question')->insertGetId($insert);
            $insert_s = [
                'dqr_dq_id' => $qid,
                'dqr_user_id' => $user_id,
                'dqr_reply_user_id' => 0,
                'dqr_content' => $content,
                'dqr_imgs' => trim($image,','),
                'dqr_time' => $today,
                'dqr_show' => 2,
                'dqr_read_uid' => ',0,',
                'dqr_del_uid' => ',0,',
            ];
            Db::table('up_company_pd_question_reply')->insert($insert_s);
            Db::commit();
            try {
                $this->Jpush(1, $uid, '你有一条新的提问消息，去处理', ['id' => $qid,'n_type' => 4]);
            } catch (\Exception $e) {
            }
            return json(['status'=>200,'msg'=>'操作成功']);
        }catch (\Exception $e) {
            Db::rollback();
            return json(['status'=>100,'msg'=>'操作失败，请稍后重试']);
        }
    }
    /**
     * 业务员查看资料提问记录之前显示列表（只有业务员有这个接口）
     * cpd_id：产品资料id
     */
    public function pdQuestionRecBefore()
    {
        extract(input());
        $subQuery = Db::table('up_company_pd_question q')
            ->join('up_u_user u','q.dq_send_uid=u.user_id','LEFT')
            ->where('dq_cpd_id',$cpd_id)
            ->where('dq_all_uid','like','%,' . $user_id . ',%')
            ->order('dq_time desc')
            ->field('q.*,user_id,user_username,user_image')
            ->buildSql();
        $data = Db::table($subQuery."a")
            ->group('dq_cpd_id,dq_send_uid')
            ->order('dq_time desc')
            ->page($page,20)
            ->select();
        if($data){
            foreach ($data as $k => $v){
                //提问的人是是否是好友
                $like = Db::table('up_friend')
                    ->where('friend_user_id', $user_id)
                    ->where('friend_other_user', $v['dq_send_uid'])
                    ->find();
                $data[$k]['friend'] = 0; //不是好友
                if($like){
                    $data[$k]['friend'] = 1; //是好友
                }
            }
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 资料提问记录
     * uid：医生看时传医生本人的user_id，业务员看时传提问的人的user_id
     * cpd_id：产品资料id
     */
    public function pdQuestionRec()
    {
        extract(input());
        $data = Db::table('up_company_pd_question q')
            ->join('up_u_user u','q.dq_send_uid=u.user_id','LEFT')
            ->where('dq_cpd_id',$cpd_id)
            ->where('dq_send_uid',$uid)
            ->order('dq_time desc')
            ->page($page,20)
            ->field('q.*,user_id,user_username,user_image')
            ->select();
        if($data){
            foreach ($data as $k => $v){
                if($v['dq_imgs']){
                    $data[$k]['dq_imgs'] = explode(',',$v['dq_imgs']);
                }else{
                    $data[$k]['dq_imgs'] = [];
                }
                $field = 'qr.*,u1.user_id uid1,u1.user_username name1,u1.user_image image1,u1.user_type';
                $field .= ',u2.user_id uid2,u2.user_username name2,u2.user_image image2';
                $reply = Db::table('up_company_pd_question_reply qr')
                    ->join('up_u_user u1','qr.dqr_user_id=u1.user_id','LEFT')
                    ->join('up_u_user u2','qr.dqr_reply_user_id=u2.user_id','LEFT')
                    ->where('dqr_dq_id',$v['dq_id'])
                    ->where('dqr_show',1)
                    ->order('dqr_time desc')
                    ->field($field)
                    ->select();
                foreach ($reply as $k2 => $v2){
                    if($v2['dqr_imgs']){
                        $reply[$k2]['dqr_imgs'] = explode(',',$v2['dqr_imgs']);
                    }else{
                        $reply[$k2]['dqr_imgs'] = [];
                    }
                }
                $data[$k]['reply'] = $reply;
            }
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 直接回复提问
     * dq_id：提问的id
     */
    public function pdQuestionComm()
    {
        extract(input());
        $today = date('Y-m-d H:i:s');
        $insert = [
            'dqr_dq_id' => $dq_id,
            'dqr_user_id' => $user_id,
            'dqr_reply_user_id' => 0,
            'dqr_content' => $content,
            'dqr_imgs' => trim($imgs,','),
            'dqr_read_uid' => ',0,',
            'dqr_del_uid' => ',0,',
            'dqr_time' => $today
        ];
        $res = Db::table('up_company_pd_question_reply')->insert($insert);
        if($res){
            $user_type = $this->getUserInfo($user_id,'user_type');
            $data = [
                'dq_answer' => 2,
                'dq_answer_time' => $today
            ];
            if($user_type == 1){
                $data['dq_answer'] = 1;
            }
            Db::table('up_company_pd_question')->where('dq_id',$dq_id)->update($data);
            try {
                $whoQ = Db::table('up_company_pd_question')->where('dq_id',$dq_id)
                    ->field('dq_send_uid,dq_all_uid')->find();
                $whoStr = $whoQ['dq_send_uid'] . $whoQ['dq_all_uid'];
                $whoStr = trim($whoStr,',');
                $whoArr = explode(',',$whoStr);
                $list = array_diff($whoArr, array($user_id));
                $this->Jpush(1, $list, '你有一条新的提问回复消息，去处理', ['id' => $dq_id,'n_type' => 4]);
            } catch (\Exception $e) {
            }
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请刷新重试']);
        }
    }
    /**
     * 回复提问的回复
     * dqr_id：回复id
     */
    public function pdQuestionReply()
    {
        extract(input());
        $today = date('Y-m-d H:i:s');
        $data = Db::table('up_company_pd_question_reply')
            ->where('dqr_id',$dqr_id)
            ->find();
        $insert = [
            'dqr_dq_id' => $data['dqr_dq_id'],
            'dqr_user_id' => $user_id,
            'dqr_reply_user_id' => $data['dqr_user_id'],
            'dqr_content' => $content,
            'dqr_imgs' => trim($imgs,','),
            'dqr_read_uid' => ',0,',
            'dqr_del_uid' => ',0,',
            'dqr_time' => $today
        ];
        $res = Db::table('up_company_pd_question_reply')->insert($insert);
        if($res){
            $user_type = $this->getUserInfo($user_id,'user_type');
            $data2 = [
                'dq_answer' => 2,
                'dq_answer_time' => $today
            ];
            if($user_type == 1){
                $data2['dq_answer'] = 1;
            }
            Db::table('up_company_pd_question')->where('dq_id',$data['dqr_dq_id'])->update($data2);
            try {
                $whoQ = Db::table('up_company_pd_question')->where('dq_id',$data['dqr_dq_id'])
                    ->field('dq_send_uid,dq_all_uid')->find();
                $whoStr = $whoQ['dq_send_uid'] . $whoQ['dq_all_uid'];
                $whoStr = trim($whoStr,',');
                $whoArr = explode(',',$whoStr);
                $list = array_diff($whoArr, array($user_id));
                $this->Jpush(1, $list, '你有一条新的提问回复消息，去处理', ['id' => $data['dqr_dq_id'],'n_type' => 4]);
            } catch (\Exception $e) {
            }
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请刷新重试']);
        }
    }
    /**
     * 回复里面的资料（？）
     * cpd_id：资料id
     */
    public function pdQuestionFiles()
    {
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_shi',2);
        $cp_id = Db::table('up_company_product_data')
            ->where('cpd_id',$cpd_id)
            ->value('cpd_cp_id');
        $where = [
            'cpd_cp_id' => $cp_id
        ];
        if($user['user_type'] == 1){
            $where['cpd_shi'] = ['like',"%,{$user['user_shi']},%"];
        }
        $data = Db::table('up_company_product_data')
            ->where($where)
            ->order('cpd_top desc,cpd_sort desc,cpd_update desc')
            ->field('cpd_id,cpd_name')
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 转给同事
     */
    public function questionChangeUser()
    {
        extract(input());
        $user_type = $this->getUserInfo($user_id,'user_type');
        if($user_type == 1){
            return json(['status'=>100,'msg'=>'您无权转让']);
        }
        $question = Db::table('up_company_pd_question')
            ->where('dq_id',$dq_id)
            ->field('dq_zhuan,dq_zhuan_uid,dq_all_uid')
            ->find();
        $today = date('Y-m-d H:i:s');
        $uids = trim($uids,',');
        $data = [
            'dq_all_uid' => $question['dq_all_uid'] . $uids . ',',
            'dq_zhuan' => 2,
            'dq_update' => $today,
        ];
        if($question['dq_zhuan'] == 1){
            $data['dq_zhuan_uid'] = ',' . $uids . ',';
        }else{
            $data['dq_zhuan_uid'] = $question['dq_zhuan_uid'] . $uids . ',';
        }
        $res = Db::table('up_company_pd_question')->where('dq_id',$dq_id)->update($data);
        if($res){
            try {
                $list = explode(',',$uids);
                $this->Jpush(1, $list, '您有一个新的提问信息转交给你进行协助处理', ['id' => $dq_id,'n_type' => 4]);
            } catch (\Exception $e) {
            }
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请刷新重试']);
        }
    }
    //----------------------项目------------------------------------------------
    /**
     * 项目详情
     */
    public function projectInfo()
    {
        extract(input());
        $field = 'p.*,cp_id,cp_name,cp_thumb,user_id,user_username,user_image';
        $data = Db::table('up_project p')
            ->join('up_company_product cp','p.pro_cp_id=cp.cp_id','LEFT')
            ->join('up_u_user u','p.pro_user_id=u.user_id','LEFT')
            ->where('pro_id',$pro_id)
            ->field($field)
            ->find();
        //--------------------------加浏览量，入浏览记录--------------------------------------
        Db::table('up_project')->where('pro_id',$pro_id)->setInc('pro_browse');
        $have_h = Db::table('up_project_history')
            ->where('ps_pro_id',$pro_id)
            ->where('ps_user_id',$user_id)
            ->find();
        if($have_h){
            Db::table('up_project_history')->where('ps_id',$have_h['ps_id'])
                ->setField('ps_time',date('Y-m-d H:i:s'));
        }else{
            $insert_s = [
                'ps_user_id' => $user_id,
                'ps_pro_id' => $pro_id,
                'ps_time' => date('Y-m-d H:i:s')
            ];
            Db::table('up_project_history')->insert($insert_s);
        }

        //----------------------------------------------------------------
        $files = Db::table('up_project_data')
            ->where('prod_pro_id',$pro_id)
            ->field('prod_id,prod_name,prod_type,prod_content,prod_thumb')
            ->order('prod_top desc,prod_sort desc,prod_update desc')
            ->select();
        if($files){
            foreach ($files as $k => $v){
                if($v['prod_type'] == 1 && $v['prod_content']){
                    $files[$k]['prod_content'] = explode(',',$v['prod_content']);
                }
            }
        }
        //收藏
        $like = Db::table('up_like_project')
            ->where('lpro_user_id', $user_id)
            ->where('lpro_type', 2)
            ->where('lpro_prom_id', $pro_id)
            ->find();
        $data['like'] = 0;
        if($like){
            $data['like'] = 1;
        }
        //参与
        $join = Db::table('up_project_user')
            ->where('pu_user_id', $user_id)
            ->where('pu_pro_id', $pro_id)
            ->field('pu_status,pu_remark,pu_time,pu_update')
            ->find();
        $data['join'] = 0;
        if($join){
            $data['join'] = $join['pu_status']; //1是待审核  2是同意  3是拒绝
            $data['join_time'] = $join['pu_time'];
            $data['join_update'] = $join['pu_update'];
            $data['join_remark'] = $join['pu_remark'];
        }
        return json(['status'=>200,'msg'=>$data,'files'=>$files]);
    }
    /**
     * 项目浏览记录
     */
    public function projectHistory()
    {
        extract(input());
        $field = 'ps_time,ps_user_id,user_id,user_username,user_image';
        $data = Db::table('up_project_history ps')
            ->join('up_u_user u','ps.ps_user_id=u.user_id','LEFT')
            ->where('ps_pro_id',$pro_id)
            ->order('ps_time desc')
            ->page($page,20)
            ->field($field)
            ->select();
        if($data){
            foreach ($data as $k => $v){
                //提问的人是是否是好友
                $like = Db::table('up_friend')
                    ->where('friend_user_id', $user_id)
                    ->where('friend_other_user', $v['ps_user_id'])
                    ->find();
                $data[$k]['friend'] = 0; //不是好友
                if($like){
                    $data[$k]['friend'] = 1; //是好友
                }
            }
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 申请参与项目
     */
    public function projectJoin()
    {
        extract(input());
        $today = date('Y-m-d H:i:s');
        //参与
        $join = Db::table('up_project_user')
            ->where('pu_user_id', $user_id)
            ->where('pu_pro_id', $pro_id)
            ->value('pu_status');
        if($join){
            return json(['status'=>100,'msg'=>'您已经申请过了']);
        }else{
            $insert = [
                'pu_pro_id' => $pro_id,
                'pu_user_id' => $user_id,
                'pu_time' => $today,
            ];
            $res = Db::table('up_project_user')->insert($insert);
            if($res){
                return json(['status'=>200,'msg'=>'申请成功，请耐心等待平台审核']);
            }else{
                return json(['status'=>100,'msg'=>'您已经申请过了']);
            }
        }
    }
}